using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace XeoEngine
{

    class BackgroundScreen : GameScreen
    {
        ContentManager content;
        Texture2D backgroundTexture;
        Texture2D circleTexture;
        Texture2D splashTexture;
        float oldsplash = 1.0f;
        float circlerot;
        bool splashdown = false;
        Cue bgcue;

        public BackgroundScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        public override void LoadContent()
        {
            if (content == null) content = new ContentManager(ScreenManager.Game.Services, "Content");
            backgroundTexture = content.Load<Texture2D>("Menu/background");
            circleTexture = content.Load<Texture2D>("Menu/Circle");
            splashTexture = content.Load<Texture2D>("Menu/Splash");
            bgcue=ScreenManager.soundBank.GetCue("BGMusic");
            bgcue.Play();
        }

        public override void UnloadContent()
        {
            bgcue.Stop(AudioStopOptions.AsAuthored);
            content.Unload();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (oldsplash >= 1.0f) splashdown = true;
            if (oldsplash <= 0.0f) splashdown = false;
            if (splashdown)
                oldsplash -= 0.0025f;
            else oldsplash += 0.0025f;
            if ((int)(gameTime.TotalGameTime.TotalSeconds % 2.125) == 0)
                circlerot -= 0.1f;
            else
                circlerot += 0.005f;
            if (circlerot > Math.PI * 2) circlerot = 0;
            base.Update(gameTime, otherScreenHasFocus, false);
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;
            Rectangle fullscreen = new Rectangle(0, 0, viewport.Width, viewport.Height);
            float scale = 1;
            spriteBatch.Begin();
            spriteBatch.Draw(backgroundTexture, fullscreen, new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha));
            spriteBatch.Draw(splashTexture, new Rectangle(-viewport.Width / 4, -viewport.Width / 4, viewport.Width, viewport.Width), new Color(TransitionAlpha * oldsplash, TransitionAlpha * oldsplash, TransitionAlpha * oldsplash));
            spriteBatch.Draw(circleTexture, new Rectangle(viewport.Width / 4, viewport.Width / 4, (int)(viewport.Width / 2 * scale), (int)(viewport.Width / 2 * scale)), circleTexture.Bounds, new Color(TransitionAlpha, TransitionAlpha, TransitionAlpha), circlerot, new Vector2(circleTexture.Bounds.Center.X, circleTexture.Bounds.Center.Y), SpriteEffects.None, 0);
            spriteBatch.End();
        }
    }
}